#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
import requests
import os,re,IPy,time
from random import choice
def miaopaiVVID():
    HEAD = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
        "Referer": "http://weibo.com/shoujipaike?c=spr_qdhz_bd_baidusmt_weibo_s&nick=%E7%A7%92%E6%8B%8D&is_hot=1",
        "Accept-Encoding": "gzip, deflate, sdch",
        "Cookie": "SINAGLOBAL=6462627917062.491.1457682672956; UM_distinctid=15b9da00b5fa-04c47933528462-3e64430f-1fa400-15b9da00b603e1; SCF=AqrqhEZM97f1SpZFG-NR7gK8ygW2vTrYwAHtTCBzb06lI5JMaFw_DWi-SYhReJFU8TXFPHz0aujUdPFu0S0QyO8.; SUHB=02M_3rwViQYvsK; ALF=1535102137; SUB=_2AkMu_1pXf8NxqwJRmP0Vym_ma4tywg7EieKYo6uMJRMxHRl-yT83qms9tRB1Xi2n66hv01CaCDZIgcC60qXiQQ..; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFQdLkixMvJ4-5gjknfPg_v; TC-Page-G0=1ac1bd7677fc7b61611a0c3a9b6aa0b4; _s_tentry=www.baidu.com; Apache=7503220955005.025.1508399176830; ULV=1508399176837:20:3:2:7503220955005.025.1508399176830:1508230263406; TC-V5-G0=634dc3e071d0bfd86d751caf174d764e; wb_cusLike_3655689037=N; UOR=www.chongdiantou.com,widget.weibo.com,spr_qdhz_bd_baidusmt_weibo_s"
    }
    r = requests.get(url="https://weibo.com/shoujipaike?ssl_rnd=1509977914.8203&is_hot=1", headers=HEAD)
    miaopaiHTM = r.text
    miaopaiVID = re.findall("show\/[^\.]+\.htm", miaopaiHTM.replace("\\",""))
    VID = choice(miaopaiVID).split("/")[1].split(".")[0]
    return VID

def CDNlist(ipC):
    Default=False
    CDN = open('CDN_IPlist.txt').read().split('\n')
    for x in CDN:
        CDN_ip=IPy.IP(x)
        boolIP=(ipC in CDN_ip)
        Default=(Default or boolIP)
    return Default

def dnsR(domian):
    ns=os.popen('nslookup % s ' % domian).read()
    if ns.find('Name:') == -1:
        return []
    else:
        ns=ns[ns.find('Name:'):]
        ip = re.findall('\d+\.\d+\.\d+\.\d+', ns)
        return ip


def miaopai(miaopai_URL):
    # r = requests.get(url='http://gslb.miaopai.com/stream/edcgx2yW3~cDhuclzXaU7jWQPOqlRPNNZ9W3tQ__.json?token=')
    #miaopai_URL = "http://www.miaopai.com/show/edcgx2yW3~cDhuclzXaU7jWQPOqlRPNNZ9W3tQ__.htm"
    try:
        aa=miaopaiVVID()
        miaopaiDD_url = ('http://gslb.miaopai.com/stream/%s.json?token=' % aa)
        r = requests.get(url=miaopaiDD_url)
        if r.status_code < 400:  # 获取返回状态
            # print(r.json())
            miaopai_R = r.json()['result'][0]['host']
            mp=r.json()['result'][0]
            video=mp["scheme"]+mp["host"]+mp["path"]
            return miaopai_R,video
        else:
            return "打开错误！","打开错误！"
    except:
        return "打开错误！","打开错误！"


def main(url):
    miaopai_domian,video = miaopai(url)
    miaopai_IP = dnsR(miaopai_domian)
    if len(miaopai_IP) == 0:
        flag = "无解析结果"
    else:
        if CDNlist(miaopai_IP[0]):
            # print(miaopai_ip)
            flag = '网内'
        else:
            flag = '出网'
    return (video,flag,"<br>".join(miaopai_IP) )


if __name__ == '__main__':
    url="http://www.miaopai.com/show/edcgx2yW3~cDhuclzXaU7jWQPOqlRPNNZ9W3tQ__.htm"

    for i in range(10):
        t=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        res = main(url)
        print('%s\t%s\t%s\n' % res)
        fLog=open('/var/www/html/short_video/%sShortVideoLog.txt'% time.strftime("%Y-%m-%d", time.localtime()),'a' )
        fLog.write(t+'\t秒拍\t%s\t%s\t%s\n' % res)
        fLog.close()



